﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TerrainRealtimePit : MonoBehaviour {
    
    public RenderTexture m_RenderTexture;
    DynamicPitManage m_DynamicPitManage;
    public float m_LifeSpan = 5;
    // Use this for initialization
    

    void Start()
    {
        m_DynamicPitManage = new DynamicPitManage(Vector3.zero, m_RenderTexture);
        m_DynamicPitManage.m_LifeSpan = m_LifeSpan;
        
    }
    // Update is called once per frame
    void Update () {

        if (m_DynamicPitManage == null) return;
        m_DynamicPitManage.Update(Time.deltaTime);
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "foot")
        {
            Vector3 position = other.transform.position / 40.0f;//40.0 is the size of the terrain

            float angle = (other.transform.rotation.eulerAngles.y) / 360.0f; //map into range 0-1
                                                                             //Debug.Log(angle.ToString());
                                                                             //call the event manager
            DoAction(position, angle);
            //play sound 
            //controller.GetComponent<AudioSource>().Play();
        }
    }

    void OnTriggerStay(Collider other)
    {
        if (other.tag == "foot")
        {
            Vector3 position = other.transform.position / 40.0f;//20.0 is the size of the terrain
            //Debug.Log(position.ToString());
            float angle = (other.transform.rotation.eulerAngles.y) / 360.0f; //map into range 0-1
                                                                             //Debug.Log(angle.ToString());
                                                                             //call the event manager
           // DoAction(position, angle);
        }

    }


    public void DoAction(Vector3 position, float angle)
    {
        if (m_DynamicPitManage == null) return;
        m_DynamicPitManage.DoAction(position, angle);
    }
    

    private void OnDestroy()
    {
        m_DynamicPitManage.Destroy();
    }
}



public class DynamicPitManage
{
    public GameObject m_Parent;

    GameObject m_Camera;
    public Camera m_SpriteCamera;
    private List<DynamicPitTexture> m_PitTexsCount;
    public float m_LifeSpan = 5;

    public DynamicPitManage(Vector3 parent,RenderTexture renderTexture)
    {
        m_Parent = new GameObject("DynamicPitManage");
        m_Parent.transform.position = parent;

        m_Camera = new GameObject("SpriteCamera");
        m_SpriteCamera = m_Camera.AddComponent<Camera>();
        m_SpriteCamera.transform.parent = m_Parent.transform;
        m_SpriteCamera.transform.position = new Vector3(0,0,-1.214f);
        m_SpriteCamera.clearFlags = CameraClearFlags.SolidColor;
        m_SpriteCamera.orthographic = true;
        m_SpriteCamera.orthographicSize = 0.5f;
        m_SpriteCamera.renderingPath = RenderingPath.VertexLit;
        m_SpriteCamera.targetTexture = renderTexture;
        m_SpriteCamera.nearClipPlane = 0;
        m_SpriteCamera.farClipPlane = 2;
        m_SpriteCamera.useOcclusionCulling = false;
        m_SpriteCamera.allowHDR = false;
        m_SpriteCamera.allowMSAA = false;

        m_PitTexsCount = new List<DynamicPitTexture>();
    }

    public void Update(float deltaTime)
    {
        foreach (var p in m_PitTexsCount)
        {
            p.Update(deltaTime);
        }
    }

    bool isHave = false;
    public void DoAction(Vector3 position, float angle)
    {
        if (m_Parent == null) return;

        isHave = false;
        DynamicPitTexture spritePitTex = null;
        foreach (var p in m_PitTexsCount)
        {
            if(!p.isUsed)
            {
                spritePitTex = p;
                isHave = true;
                break;
            }
        }

        GameObject spriteObj = null;

        if (!isHave)
        {
            spriteObj = (GameObject)GameObject.Instantiate(Resources.Load("DynamicPitTexture"));
            spritePitTex = new DynamicPitTexture();
            m_PitTexsCount.Add(spritePitTex);
        }
       
        spritePitTex.Init(spriteObj, m_LifeSpan);

        spritePitTex.m_Obj.transform.parent = m_Parent.transform;
        spritePitTex.m_Obj.transform.localPosition = new Vector3(position.x - 0.5f, position.z - 0.5f, 0);

        Color positionInfo = new Color(spritePitTex.m_Obj.transform.localPosition.x + 0.5f, angle, spritePitTex.m_Obj.transform.localPosition.y + 0.5f, 1.0f);
        spritePitTex.m_PitTex.color = positionInfo; 
    }

    public void Destroy()
    {
        foreach (var p in m_PitTexsCount)
        {
            p.Destroy();
        }
        m_PitTexsCount.Clear();
        m_PitTexsCount = null;
        GameObject.DestroyImmediate(m_Camera);
        GameObject.DestroyImmediate(m_Parent);
    }
}


public class DynamicPitTexture
{
    public GameObject m_Obj;
    public SpriteRenderer m_PitTex;
    public float m_LifeSpan = 10;
    public float alpha = 1;
    private float timer;
    public bool isUsed = false;

    public DynamicPitTexture()
    {
        
    }

    public void Init(GameObject obj = null, float lifeSpan = 10)
    {
        if(m_Obj == null)
        {
            if (obj == null) Debug.LogError("m_Obj and obj 不能同时为空");

            m_Obj = obj;
            m_PitTex = m_Obj.GetComponent<SpriteRenderer>();
        }

        m_Obj.SetActive(true);
        m_LifeSpan = lifeSpan;
        timer = m_LifeSpan;
        isUsed = true;
    }

    public void Update(float deltaTime)
    {
        if (!isUsed) return;

        timer -= Time.deltaTime;
        if (timer <= 0.0f)
        {
            Disable();
        }
        else
        {
            alpha = timer / m_LifeSpan;
            m_PitTex.color = new Color(m_PitTex.color.r, m_PitTex.color.g, m_PitTex.color.b, alpha);
        }
    }

    public void Disable()
    {
        isUsed = false;
        timer = m_LifeSpan;
        m_Obj.SetActive(false);
    }

    public void Destroy()
    {
       GameObject.DestroyImmediate(m_Obj);
    }

}


